package com.ordertracker.service;

import com.ordertracker.Application;
import com.ordertracker.Session;
import com.ordertracker.dao.GeneralCrudDao;
import com.ordertracker.exception.ValidationException;
import com.ordertracker.persistent.dictionary.Position;
import com.ordertracker.protocol.CommandMessage;
import com.ordertracker.to.TransferObjectFactory;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBException;
import java.io.StringReader;

/**
 * Description of class.
 * Created 2/5/13 1:59 PM
 *
 * @author andrey.rodin@playtech.com
 */

@Service(value = "position")
public class PositionManager extends AbstractCRUDEventHandle<Position> {

    private static Logger logger = Logger.getLogger(PositionManager.class);

//    private HandleChain chain;
//
    private GeneralCrudDao<Position, Long> dao;
//
    private TransferObjectFactory transferObjectFactory;

//    @Resource(name = "defaultMan")
//    public void setChain(HandleChain chain) {
//        this.chain = chain;
//    }
//
    @Resource(name = "crudDao")
    public void setDao(GeneralCrudDao<Position, Long> dao) {
        this.dao = dao;
    }
//
    @Resource(name = "tof")
    public void setTransferObjectFactory(TransferObjectFactory transferObjectFactory) {
        this.transferObjectFactory = transferObjectFactory;
    }

    @Override
    protected Position unmarshal(String obj) {
        com.ordertracker.entity.Position position = null;
        StringReader sr = new StringReader(obj);
        System.out.println("Position manager: create: "+sr);
        try {
            position = Application.getApplication().getJsonUnmarshaller().unmarshalFromJSON(sr,
                    com.ordertracker.entity.Position.class);
        } catch (JAXBException e) {
            e.printStackTrace();
        }

        return transferObjectFactory.positionFromDTO(position);
    }

    @Override
    protected String getServiceName() {
        return "position";
    }

    @Override
    protected void validate(Session session, Position msg, ActionType type) throws ValidationException {
//        throw new ValidationException(Response.status(Response.Status.BAD_REQUEST).build());
    }

    @Override
    protected Response otherAction(Session session, CommandMessage msg, Position obj, ActionType type) {
        //TODO: Here we can handle other actions  e.d. GET_ALL_LIST, GET_BY_CRITERIA and so on.
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected boolean needToUnmarshal(ActionType type) {
        switch (type) {
            case ADD:
            case UPDATE:
                return true;
            default:
                return false;
        }
    }

    @Override
    protected com.ordertracker.entity.Position create(Session session, Position msg) {
        Position position = dao.saveEntity(msg);
        return transferObjectFactory.positionToDTO(position);
    }

    @Override
    protected Position receive(CommandMessage msg) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected Position delete(Session session, Position msg) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected Position update(Session session, Position msg) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

}
